<template>
  <div>
    <PurchaseOrderTable ref="refOrderList" :tabsValue="tabsValue" :statusList="statusList"
                        @openVoucherModal="openVoucherModal" @warehouseShipping="warehouseShipping"/>
    <!--  上传购买凭证  -->
    <el-dialog title="编辑购买凭证" :visible.sync="isOpenUploadVocher" width="670px">
      <div class="edit-box">
        <el-form size="small" :model="purchaseVoucherInfo" :rules="purchaseVoucherInfoRules" ref="purchaseVoucherRef"
                 label-width="120px">
          <el-form-item label="购买凭证：" prop="proof">
            <div v-if="tabsValue=='all'">
              <image-preview :src="purchaseVoucherInfo.proof"></image-preview>
            </div>
            <div v-else>
              <image-upload v-model="purchaseVoucherInfo.proof" :is-show-tip="false" width="50px"
                            height="50px"/>
            </div>
          </el-form-item>
          <el-form-item label="采购价格：" prop="purchasePrice">
            <el-input :disabled="tabsValue=='all'" v-model="purchaseVoucherInfo.purchasePrice"
                      placeholder="请输入采购价格"/>
          </el-form-item>
          <el-form-item label="发货类型：" prop="type">
            <el-select :disabled="tabsValue=='all'" v-model="purchaseVoucherInfo.type" placeholder="请选择发货类型"
                       size="small">
              <el-option v-for="(item,index) in dict.type.send_out_type" :key="index" :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isOpenUploadVocher = false">取 消</el-button>
        <el-button v-if="tabsValue !='all' " type="primary" @click="confirmVocher()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import PurchaseOrderTable from "@/views/purchase/components/PurchaseOrderTable.vue";
import {getWarehouseProBySkuCode, querySendGoodsInfo, uploadProof} from "@/api/order/purchaser";

export default {
  name: "pendPurchaseOrder",
  dicts: ['send_out_type'],
  components: {
    PurchaseOrderTable,
  },
  data() {
    return {
      tabsValue: "103",//待采购
      statusList: ['103'],
      isOpenUploadVocher: false,//是否打开上传凭证模态框
      purchaseVoucherInfo: {},//购买凭证信息
      purchaseVoucherInfoRules: {
        proof: [{required: true, message: '请上传购买凭证', trigger: 'change'}],
        purchasePrice: [{required: true, message: '采购价格不能为空', trigger: 'blur'}],
        type: [{required: true, message: '请选择发货类型', trigger: 'change'}],
      },
      orderId: "",
    }
  },
  created() {

  },
  methods: {
    // 打开上传凭证弹窗
    openVoucherModal(row) {
      this.orderId = row.orderId
      this.childOrderId = row.childOrderId
      this.reset()
      this.getSendGoodsInfo()
    },
    // 获取已上传的凭证和运单号信息
    getSendGoodsInfo() {
      querySendGoodsInfo({childOrderId: this.childOrderId}).then(res => {
        let data = res.data
        if (data) {
          this.purchaseVoucherInfo = data
          this.purchaseVoucherInfo.type = data.type.toString()
          if (this.purchaseVoucherInfo.type == '0') {
            this.purchaseVoucherInfo.type = undefined
          }
          this.isOpenUploadVocher = true
        }
      })
    },
    // 重置
    reset() {
      this.purchaseVoucherInfo = {
        type: undefined,
        proof: undefined,
        purchasePrice: undefined
      }
    },
    // 确定上传凭证
    confirmVocher() {
      this.$refs['purchaseVoucherRef'].validate((valid) => {
        if (valid) {
          this.purchaseVoucherInfo.orderId = this.orderId
          this.purchaseVoucherInfo.childOrderId = this.childOrderId
          uploadProof(this.purchaseVoucherInfo).then(res => {
            this.$message.success(res.msg)
            this.isOpenUploadVocher = false
          })
        }
      })
    },
    // 仓储发货
    warehouseShipping(row) {
      this.$prompt('请输入库存编码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /([^\s])/,
        inputErrorMessage: '库存编码不能为空'
      }).then(({value}) => {
        let dataList = [];
        this.sellerOrderList.forEach((item) => {
          if (item.orderId == row.orderId) {
            let childOrdrList = item.sellerChildOrderList;
            childOrdrList.forEach((childItem) => {
              dataList.push({
                orderId: row.orderId,
                childOrderId: childItem.childOrderId,
                skuCode: childItem.skuCode,
                trackingNumber: value
              })
            })
          }
        })
        getWarehouseProBySkuCode(dataList).then(res => {
          this.$message.success(res.msg)
          this.$refs.refOrderList.getList()
        })
      })
    },
  }
}
</script>

<style scoped>

</style>
